<script setup>
import {
  articleGetArticleSortService,
  articleDelChannelService
} from '@/api/article'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './conponents/ChannelEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const articleSortList = ref([])
const loading = ref(false)
const dialog = ref()
//获取数据
const getArticleSort = async () => {
  loading.value = true
  const res = await articleGetArticleSortService()
  articleSortList.value = res.data.data
  loading.value = false
}
getArticleSort()
//编辑
const onEditChannel = (row) => {
  dialog.value.open(row)
}
//删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确定要删除该分类么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
  console.log(row.id)
  const res = await articleDelChannelService(row.id)
  console.log(res)
  ElMessage.success('删除成功')
  getArticleSort()
}
//添加
const onAddChannel = () => {
  dialog.value.open({})
}
//刷新页面
const onSuccess = () => {
  getArticleSort()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <div>
        <el-button :icon="Edit" type="primary" round @click="onAddChannel"
          >添加分类</el-button
        >
      </div>
    </template>
    <el-table
      v-loading="(loading = loading)"
      :data="articleSortList"
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="180"></el-table-column>
      <el-table-column prop="articleSortTitle" label="名称"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            type="primary"
            round
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            type="danger"
            round
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>

<style lang="less" scoped></style>
